package logic

import (
	"context"
	"friend/common"
	"friend/models"

	"friend/internal/svc"
	"friend/pb/friend"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGroupLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddGroupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGroupLogic {
	return &AddGroupLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AddGroup 好友分组新增
func (l *AddGroupLogic) AddGroup(in *friend.GroupInfo) (*friend.Empty, error) {
	result, err := l.svcCtx.FriendGroupModel.Insert(l.ctx, &models.FriendGroup{
		UserId: in.UserId,
		Name:   in.Name,
		Status: common.FriendGroupStatusNormal,
	})
	if err != nil {
		return nil, err
	}

	lastID, err := result.LastInsertId()
	if err != nil {
		return nil, err
	}

	in.Id = lastID
	l.Logger.Infof("add group: %v", in)
	return &friend.Empty{}, nil
}
